package net.citrite.android.logger;

import java.util.Date;
import java.util.logging.LogRecord;

/**
 * Created by taodongl on 4/29/15.
 */
public class TruncationFormatter extends java.util.logging.Formatter {
    @Override
    public String format(LogRecord record) {
        StringBuffer sb = new StringBuffer();
        sb.append(appendISO8601(record.getMillis()));
        sb.append("\t");
//        sb.append(record.getSourceClassName());
//        sb.append(".");
        sb.append(record.getSourceMethodName());
        sb.append(" [");
        sb.append(record.getThreadID());
        sb.append("] ");
        sb.append(formatMessage(record));
        sb.append("\n");
        return sb.toString();
    }

    private void a2(StringBuffer sb, int x) {
        if (x < 10) {
            sb.append('0');
        }
        sb.append(x);
    }

    // Append the time and date in ISO 8601 format
    @SuppressWarnings("deprecation")
    private String appendISO8601(long millis) {
        StringBuffer sb = new StringBuffer();
        Date date = new Date(millis);
        sb.append(date.getYear() + 1900);
        sb.append('-');
        a2(sb, date.getMonth() + 1);
        sb.append('-');
        a2(sb, date.getDate());
        sb.append('T');
        a2(sb, date.getHours());
        sb.append(':');
        a2(sb, date.getMinutes());
        sb.append(':');
        a2(sb, date.getSeconds());
        return sb.toString();
    }
}
